
#include "pointMotion.h"

/*
moveLaw _moveLaws[8]=
{
	{ 0,+1},	 	
	{ 0,-1},
	{+1, 0},
	{-1, 0},
	{+1,+1},
	{-1,-1},
	{-1,+1},
	{+1,-1}

};
*/


char nextStep[3][2];



 





struct _moveLaw
{
	char a;
	char b;
};
typedef struct _moveLaw moveLaw;

typedef struct _point point_t;
void
writePointPosition(point_t* pPoint, char newX, char newY)
{
	char mask;
	
	mask=1;
	mask <<= (pPoint->x%8);
	drawTbl[(pPoint->x/8)*96 + pPoint->y] ^= mask;
	
	mask=1;
	mask <<= (newX%8);
	drawTbl[(newX/8)*96 + newY] ^= mask;
	
	pPoint->x = newX;
	pPoint->y = newY;
}


//
//find the next position of 
//


void findNextStep(point_t* pPoint, moveLaw *mLaw, int i, int nextStepIndex)
{
char xInd,yInd,xLaw, yLaw;


	xInd = pPoint->x;
	yInd = pPoint->y;
	xLaw = (mLaw + i )-> a ;
	yLaw = (mLaw + i )-> b ;

	 //	nextStep[nextStepIndex][0] = *(pPoint ->x) + *((mLaw+i)-> a);	
	 //	nextStep[nextStepIndex][1] = *(pPoint ->y) + *((mLaw+i)-> b);	
		nextStep[nextStepIndex][0] = xInd + xLaw;	
		nextStep[nextStepIndex][1] = yInd + yLaw;	

}


//
// check for coliision between display bound and point
//
	
int CollisionCheckBound(int nextStepIndex)
{
	if (nextStep[nextStepIndex][0]>95 || nextStep[nextStepIndex][0]<0 ||
	  nextStep[nextStepIndex][1]>15 || nextStep[nextStepIndex][1]<0)
	  return 1;
	  else 
	  return 0;
}




//
// check for coliision between two points
//

int CollisionCheckPoint()
{
int i=0;
	
	if (  (nextStep[i][0] == nextStep[i++][0] && nextStep[i][1] == nextStep[i++][1])
	||  (nextStep[i][0] == nextStep[i+2][0] && nextStep[i][1] == nextStep[i+2][1])
	|| 	(nextStep[i++][0] == nextStep[i+2][0] && nextStep[i++][1] == nextStep[i+2][1]) )

	return 1;
	else
	return 0;
}



//
// change the point's direction  if  point touch tbe bound of display
//



void 
changePosition (point_t* pPoint, moveLaw *mLaw, int i)
{
	
 	char xInd,yInd;
	int nextStepIndex=0;		// Index for the point number
 	
	findNextStep(pPoint, mLaw,  i, nextStepIndex);
	if (CollisionCheckBound(nextStepIndex))
	{
	
	  if ((nextStep[nextStepIndex][0]<95 || nextStep[nextStepIndex][0]>0 ) &&
	 ( nextStep[nextStepIndex][1]>15 || nextStep[nextStepIndex][1]<0) && (i== 5 || i== 7))
	  {
	  if (i==5)
	  	mLaw+2;
		else
		mLaw-2;
	  }
	  else
	  if ( (nextStep[nextStepIndex][0]>95 || nextStep[nextStepIndex][0]<0 ) &&
	  (nextStep[nextStepIndex][1]<15 || nextStep[nextStepIndex][1]>0) && (i== 6 || i== 7))
	  {
	  if (i==6)
	  	mLaw++;
		else
		mLaw--;
	  }
	
	else
	  if ((nextStep[nextStepIndex][0]>95 || nextStep[nextStepIndex][0]<0 ) || 
	  (nextStep[nextStepIndex][1]>15 || nextStep[nextStepIndex][1]<0) )
		{
		if (i %2 == 0 )	 
		mLaw++;
		else
		mLaw--;
		}
	}

//	 xInd =  (mLaw + i )-> a ;
//	 yInd =  (mLaw + i )-> b ;

//	 writePointPosition(pPoint, xInd, yInd);

	writePointPosition(pPoint, nextStep[nextStepIndex][0], nextStep[nextStepIndex][1]);


}


